<?php defined('SYSPATH') or die('No direct script access.');
$mydatetime	= Helper_MyDateTime::factory();
$arraylist	= Helper_ArrayList::factory();
$thesystems	= Helper_thesystems::factory();
$display		= Helper_DisplayItems::factory();
$officefunctions	= Helper_officefunctions::factory();

$r = Request::current();
$action = $r->uri2();
$mode = Arr::get($_REQUEST,'mode');
$view = Arr::get($_REQUEST,'view');
?>
<?php if($mode != 'print') { ?>
<?php 
	echo View::factory('module/apps_breadcrumb')
		->set('breadcrumbs',
			array(
				0=>array(
					'address'=>'index',
					'text'=>'Dashboard'
				),
				1=>array(
					'address'=>NULL,
					'text'=>"Report Invoice"
				)
			)
		)
		->render();
?>

<?php echo View::factory('module/apps_message')->render(); ?>
<?php } ?>
<!-- Forms Starts -->
<div class="row-fluid">
	<?php if(empty($view)) { ?>
		<div class="span6">
				
				<!-- User Data Starts -->
				<div class="row-fluid"> 
					<?php echo Form::open($action.URL::query(array('view'=>'1')), array('class'=>'form-horizontal','enctype'=>'multipart/form-data','name'=>'updatePost', 'onSubmit'=>'return checkPost()'))?>
					<fieldset class="well">
					  <legend>
							 Report Invoices
							<?php //echo Form::hidden('command','search')?>
						</legend>
					  
					  <div class="control-group">
							<label class="control-label" for="tglawal">Tgl Awal</label>
							<div class="controls">
					    <?php
								echo Form::input('tglawal','',
								array
								(
									'id'=>'tglawal',
									'class'=>'span4',
									'placeholder'=>'Enter Tgl Awal'
								)
							);
							?>
							<script>
							  AnyTime.picker( "tglawal",
							    { format: "%Y-%m-%d"} );
							</script>
							</div>
						</div>
						
					  <div class="control-group">
							<label class="control-label" for="tglakhir">Tgl Akhir</label>
							<div class="controls">
					    <?php
								echo Form::input('tglakhir','',
								array
								(
									'id'=>'tglakhir',
									'class'=>'span4',
									'placeholder'=>'Enter Tgl Akhir'
								)
							);
							?>
							<script>
							  AnyTime.picker( "tglakhir",
							    { format: "%Y-%m-%d"} );
							</script>
							</div>
						</div>

					<?php echo Form::submit('submit','submit',array('class'=>'btn btn-inverse btn-primary btn-block')) ?>
					</fieldset>
					<?php echo Form::close()?>
			</div>
			</div>

	<?php } else { ?>
<!-- Product Table Starts -->
		<div class="row centered-margin">		
			<div class="box">
				<div class="box-content">
					<?php $class_table = ($mode != 'print' ? 'table table-bordered table-condensed table-hover' : 'table table-bordered table-condensed' ) ?>
					<table class="<?php echo $class_table ?>">
					  <thead>
						  <tr>
							  <th width="80">Date</th>
							  <th width="130">Invoice Number</th>
							  <th width="250">Payto</th>
							  <th width="85">Seller Status</th>
							  <th>Transaction</th>
							  <?php if($mode != 'print') { ?>
							  <th width="100">Users</th>
							  <th width="30">Paid</th>
							  <?php } ?>
						  </tr>
					  </thead>   
					  <tbody bgcolor="white">
						<?php if(empty($invoice_lists)) { ?>
						  <tr valign="middle">
						    <td colspan="8" valign="middle" style="text-align: center;"><strong>There is no data</strong></td>
						  </tr>
						<?php } else { ?>
						  <?php foreach($invoice_lists as $invoice_list) { ?>
							<tr>
								<?php $sellerstatus = $invoice_list['sellerstatus']; ?>
								<?php 
								if($sellerstatus == 1) {
									$harga = 'Harga 1';
								} elseif ($sellerstatus == 2) {
									$harga = 'Harga 2';
								} elseif ($sellerstatus == 3) {
									$harga = 'Harga 3';
								} elseif ($sellerstatus == 4) {
									$harga = 'Harga 4';
								}
								?>
								<td><?php echo $mydatetime->mktime_from_sql($invoice_list['datetime']); ?></td>
								<td><?php echo $invoice_list['invoicenumbers']; ?></td>
								<td><?php echo $invoice_list['payto']; ?></td>
								<td><?php echo $harga; ?></td>
								<td style="padding: 0px; padding-bottom: 0px;">
									
									<?php //$transaction = $officefunctions->show_transaction($invoice_list['id']) ?>
									<table class="table" style="margin-bottom: 0;">
										<tr style="font-weight: bold;">
											<td width="120" style="border-left: 0; padding: 1px; text-align: center;"><small><strong>Product</strong></small></td>
											<td width="40" style="padding: 1px; text-align: center;"><small><strong>Unit</strong></small></td>
											<td width="70" style="padding: 1px; text-align: center;"><small><strong>Discount</strong></small></td>
											<td width="80" style="padding: 1px; text-align: center;"><small><strong>Ammount</strong></small></td>
										</tr>
										<?php $total_ammounts  = 0; ?>
										<?php $total_discounts = 0; ?>

										<?php foreach($invoice_list['transaction'] as $transaction) { ?>

										<?php $ammounts        = $transaction['ammounts'] ?>
										<?php $discounts       = $transaction['discounts'] ?>
										<?php $total_ammounts  += $ammounts; ?>
										<?php $total_discounts += $discounts; ?>

										<tr>
											<?php $type  = $thesystems->system_auth('module_products_detail_type') ?>
											<?php $table = ($type == 'skus' ? 'skus' : 'ucts') ?>
											<?php $view  = ($type == 'skus' ? 'value' : 'name') ?>
											<td><?php echo $display->show_value($transaction['items'],'prod'.$table,$view) ?></td>
											<td><?php echo $transaction['totalitems'] ?></td>
											<td><?php echo number_format($discounts,0) ?></td>
											<td><?php echo number_format($ammounts,0) ?></td>
										</tr>
										<?php } ?>
										<tr>
											<td colspan="3" style="text-align: right; padding: 2px;"><strong><small>total :</small></strong></td>
											<td style="padding: 2px;">&nbsp;<strong><?php echo number_format($total_ammounts,0) ?></strong></td>
										</tr>
										<tr>
											<td colspan="3" style="text-align: right; padding: 2px;"><strong><small>global discount :</small></strong></td>
											<td style="padding: 2px;">&nbsp;<strong><?php echo number_format($invoice_list['globaldiscount'],0); ?></strong></td>
										</tr>
										<tr>
											<td colspan="3" style="text-align: right; padding: 2px;"><strong><small>grand total :</small></strong></td>
											<td style="padding: 2px;">&nbsp;<strong><?php echo number_format($total_ammounts - $invoice_list['globaldiscount'],0); ?></strong></td>
										</tr>
									</table>

								</td>
								<?php if($mode != 'print') { ?>
								<td><?php echo $display->show_value($invoice_list['users_id'],'users','fullname'); ?></td>
								<td><?php echo HTML::image('assets/global/images/'.$display->yesno_value($invoice_list['paid'])); ?></td>
								<?php } ?>
							</tr>
							<tr>
								<td colspan="7" height="5" style="padding: 0; border-left: 0; border-right: 5; border-bottom: 0;"></td>
							</tr>
						  <?php } ?>
						<?php } ?>
					  </tbody>
					</table>
					<?php if($mode != 'print') { ?>
					<a href="<?php echo 'reportinvoices'.URL::query(array('mode'=>'print','tglawal'=>$tglawal,'tglakhir'=>$tglakhir)) ?>" target="_new" title="Print"><?php echo HTML::image('assets/global/images/print.png')?></a>
					<?php } ?>
				</div>
			</div>
		</div>
		<?php } ?>
		<!-- Product Table End -->
</div>